package com.fengye.lock.zk.controller;


import com.fengye.lock.zk.domain.UserReg;
import com.fengye.lock.zk.dto.UserRegDTO;
import com.fengye.lock.zk.response.BaseResponse;
import com.fengye.lock.zk.response.StatusCode;
import com.fengye.lock.zk.service.UserRegService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @author sky
 * @date 2021年06月15日 9:37 下午
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserRegController {

    @Autowired
    UserRegService userRegService;

    @PostMapping(value = "/reg/zk", consumes = MediaType.APPLICATION_JSON_VALUE)
    public BaseResponse reg(@RequestBody UserRegDTO userDTO) {
        log.info("controller 的reg参数为：{}", userDTO);
        if (!StringUtils.hasText(userDTO.getUsername()) || !StringUtils.hasText(userDTO.getPassword())) {
            return new BaseResponse(StatusCode.InvalidParams);
        }

        BaseResponse response = new BaseResponse(StatusCode.Success);

        try {
            userRegService.registerWithZkLock(userDTO);

        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(), e.getMessage());
        }

        return response;
    }

    @GetMapping("/find/{username}")
    public UserReg getUserReg(@PathVariable String username) {
        UserReg reg = userRegService.selectByUsername(username);
        return reg;

    }
}
